package com.ynnu.dsc.controller;

import com.jspsmart.upload.SmartUploadException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import java.util.Arrays;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @author: Du Shun Chang
 * @title: NewServlet
 * @date: 2022/3/29 9:34
 * @description: TODO
 * @version：1.0
 */
public class NewServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setCharacterEncoding("utf-8");
//        req.setCharacterEncoding("uft-8");
//        SmartUpload su = new SmartUpload();
        resp.setContentType("text/html;charset=utf-8");
        String usernames = req.getParameter("username");
        String passwords = req.getParameter("password");
        String sexs = req.getParameter("sex");
        String addresss=req.getParameter("address");
        String xueyuans=req.getParameter("xueyuan");
        String myselfs=req.getParameter("myself");

            DiskFileItemFactory factory=new DiskFileItemFactory();
            ServletFileUpload upload=new ServletFileUpload(factory);
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;utf-8");
            String user = null;
            String path = null;
            //设置缓冲区大小与临时文件目录
            factory.setSizeThreshold(1024*1024*10);
            File uploadTemp=new File("C:\\Users\\shunc\\Desktop\\img");
            uploadTemp.mkdirs();
            factory.setRepository(uploadTemp);
            //设置单个文件大小限制
            upload.setFileSizeMax(1024*1024*10);
            //设置所有文件总和大小限制
            upload.setSizeMax(1024*1024*30);
            try {
                List<FileItem> list=upload.parseRequest(req);
                System.out.println(list);
                for (FileItem fileItem:list){
                    if (!fileItem.isFormField()&&fileItem.getName()!=null&&!"".equals(fileItem.getName())){
                        String filName=fileItem.getName();
                        //利用UUID生成伪随机字符串，作为文件名避免重复
                        String uuid= UUID.randomUUID().toString();
                        //获取文件后缀名
                        String suffix=filName.substring(filName.lastIndexOf("."));
                        //获取文件上传目录路径，在项目部署路径下的upload目录里。若想让浏览器不能直接访问到图片，可以放在WEB-INF下
                        String uploadPath=req.getSession().getServletContext().getRealPath("/webapp/upload");
                        File file=new File(uploadPath);
                        file.mkdirs();
                        //写入文件到磁盘，该行执行完毕后，若有该临时文件，将会自动删除
                        fileItem.write(new File(uploadPath,uuid+suffix));
                        path = "http://localhost:8080"+req.getContextPath()+"/webapp/upload/"+uuid+suffix;
                        System.out.println(path);
                        req.getServletContext().setAttribute("path",path);


                    }

                }

            }  catch (Exception e) {
                e.printStackTrace();
            }
//        com.jspsmart.upload.SmartUpload su = new   com.jspsmart.upload.SmartUpload();
//       su.initialize(index.jsp);
//        su.service(request, response);
//        su.setTotalMaxFileSize(100000000);
//        su.setAllowedFilesList("zip,rar");
//        try {
//            su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        try {
//            su.upload();
//        } catch (SmartUploadException e) {
//            e.printStackTrace();
//        }

//        String Name = su.getRequest().getParameter("Name");
//        String TYPE_ID = su.getRequest().getParameter("Type");
        req.setAttribute("username",usernames);
        req.setAttribute("password",passwords);
        req.setAttribute("sex", sexs);
        req.setAttribute("address",addresss);
        req.setAttribute("xueyuan",xueyuans);
        req.setAttribute("myself",myselfs);
        System.out.println(usernames+passwords+sexs+addresss+xueyuans+myselfs);
        req.getRequestDispatcher("blog.jsp").forward(req, resp);

        }
    }
